package com.jh.fcsm.controller.sys;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.SysLogRecord;
import com.jh.fcsm.beans.sys.vo.SysLogRecordExportVo;
import com.jh.fcsm.beans.sys.vo.SysLogRecordVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.service.sys.SysLogService;
import com.jh.fcsm.util.poi.EasyExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统日志
 *
 * @author szx
 * @date 2019-11-04 14:33:24
 */
@RestController
@RequestMapping("/sys/accesslog")
@Api(value = "系统日志", tags = "系统日志")
public class SystemLogController extends BaseController {

    private static final String PER_PREFIX = "btn:sys:log:";
    @Autowired
    private SysLogService sysLogService;

    /**
     * 分页查询用户操作日志表
     *
     * @param sysLogRecordVo 用户操作日志表 查询条件
     * @return RestApiResponse<?>
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询用户操作日志表", notes = "分页查询用户操作日志表")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@RequestBody SysLogRecordVo sysLogRecordVo) {
        PageInfo<SysLogRecord> sysLogOper = sysLogService.findPageByQuery(sysLogRecordVo);
        return RestApiResponse.ok(sysLogOper);
    }

    /**
     * 分页查询用户操作日志表
     *
     * @param id 用户操作日志表 查询条件
     * @return RestApiResponse<?>
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询日志", notes = "查询日志")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam(name = "id") String id) {
        SysLogRecord record = sysLogService.findById(id);
        return RestApiResponse.ok(record);
    }

    /**
     * 导出访问日志
     *
     * @param sysLogRecordVo void
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出访问日志", notes = "导出访问日志")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "export')")
    public void export(SysLogRecordVo sysLogRecordVo) {
        List<SysLogRecordExportVo> list = sysLogService.export(sysLogRecordVo);
        EasyExcelUtils.exportExcel(list, "导出访问日志结果", SysLogRecordExportVo.class, "导出访问日志结果", response);
    }

}
